<template>
    <view class="xilu">
        <u-navbar :safeAreaInsetTop="true" :placeholder="true" bgColor="#fff" autoBack="true">
            <view slot="center" class="xilu_center_slot flex-box">
                <input class="fs24 flex-grow-1" v-model="q" placeholder="请输入搜索的内容" placeholder-class="cola" />
                <view class="xilu_search_btn btn1" @click="search()">搜索</view>
            </view>
        </u-navbar>
			<mescroll-body ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback" :down="downOption" >
				<view class="p30">
					<view class="fs34 col3 pb35">分享生活</view>
					<view class="flex-box pb40"  v-for="(item,index) in topicList"  @click="bindTopicDetail(item.id)">
						<image :src="item.image_text" mode="aspectFill" class="xilu_pic"></image>
						<view class="flex-grow-1 pl20">
							<view class="fs30 col3">{{item.name}}</view>
							<view class="fs24 cola">{{item.join_users}}人讨论</view>
						</view>
						<view class="btn2" @click.stop="bindJoin(index)" v-if="!item.is_join">+ 加入</view>
						<view class="btn2" @click.stop="bindJoin(index)" v-else>已加入</view>
					</view>
				</view>
			</mescroll-body>
    </view>
</template>

<script>
	// 引入mescroll-mixins.js
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
    export default {
		mixins: [MescrollMixin], // 使用mixin
        data() {
            return {
				downOption: {
				    autoShowLoading: true, // 显示下拉刷新的进度条
				    textColor: "#aaa" // 下拉刷新的文本颜色
				},
				q:'',
				topicList:[],
				
            };
        },
		onLoad() {
			
		},
		methods:{
			search(){
				this.mescroll.resetUpScroll();
			},
			/*上拉加载的回调*/
			upCallback(page) {
				// 此处可以继续请求其他接口
				let pageNum = page.num; // 页码, 默认从1开始
				let pageSize = page.size; // 页长, 默认每页10条
				this.$core.get({
					url: 'xilumarket.topic/lists',
					data:{q: this.q,page: pageNum,pageSize:pageSize},
					success: (ret) => {
						let data = ret.data;
						// 接口返回的当前页数据列表 (数组)
						let curPageData = data.data;
						// 接口返回的当前页数据长度 (如列表有26个数据,当前页返回8个,则curPageLen=8)
						let curPageLen = curPageData.length;
						// 接口返回的总页数 (如列表有26个数据,每页10条,共3页; 则totalPage=3)
						let totalPage = data.last_page;
						// 接口返回的总数据量(如列表有26个数据,每页10条,共3页; 则totalSize=26)
						//let totalSize = data.total;
						// 接口返回的是否有下一页 (true/false)
						//let hasNext = data.current_page;
				
						//设置列表数据
						if (page.num == 1) this.topicList = []; //如果是第一页需手动置空列表
						this.topicList = this.topicList.concat(curPageData); //追加新数据
				
						// 请求成功,隐藏加载状态
						//方法一(推荐): 后台接口有返回列表的总页数 totalPage
						this.mescroll.endByPage(curPageLen, totalPage);
				
						
						setTimeout(() => {
							this.mescroll.endSuccess(curPageLen)
						}, 20)
					},
					fail: () => {
						//  请求失败,隐藏加载状态
						this.mescroll.endErr()
					}
				})
			},
			//进入话题
			bindTopicDetail(id){
				uni.navigateTo({
					url: '/discovery/trends_circle/trends_circle?id='+id
				})
			},
			//加入
			bindJoin(index){
				if(!this.$core.getUserinfo(true)){
					return true;
				}
				let id = this.topicList[index].id
				this.$core.post({url: 'xilumarket.topic/toggle_join',data: {topic_id: id},success: ret => {
							this.topicList[index].is_join = ret.data.is_join;
							this.$forceUpdate();
					},fail: err => {
							
					}
				});
			},
			
		}
    }
</script>

<style lang="scss" scoped>
    .xilu {
        &_center_slot {
            width: 460rpx;
            height: 75rpx;
            background: #FFFFFF;
            border-radius: 40rpx;
            border: 2rpx solid #555555;
            margin-left: 96rpx;
            padding: 0 12rpx 0 20rpx;
        }

        &_search_btn {
            width: 100rpx;
            height: 56rpx;
            font-size: 24rpx;
            line-height: 56rpx;
        }

        &_pic {
            width: 75rpx;
            height: 75rpx;
            border-radius: 10rpx;
        }

        .btn2 {
            width: 110rpx;
            height: 56rpx;
            line-height: 56rpx;
            font-size: 24rpx;
        }
    }
</style>
